﻿using System;
using Common.Logging;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.ByteCode.LinFu;
using Mayfair.Core.Common.Transaction.NHibernate;

namespace Mayfair.Core.Common.Service.NHibernate
{
    public class NHibernateBasicService : BasicService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(NHibernateBasicService));
        private SessionBuilder builder = new SessionBuilder();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pk"></param>
        /// <returns></returns>
        public override T FindByPK<T>(object pk)
        {
            //ISession session = NHibernateUtils.GetSession();
            ISession session = builder.getSession();
            T ret = session.Load<T>(pk);
            session.Close();
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objects"></param>
        public override void Insert<T>(params T[] objects)
        {
            ISession session = builder.getSession();
            using (ITransaction transaction = session.BeginTransaction())
            {
                try
                {
                    foreach (T o in objects)
                    {
                        session.Save(o);
                    }
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    log.Warn(ex.Message, ex);
                    if (session != null)
                    {
                        if (transaction.IsActive == true)
                        {
                            transaction.Rollback();
                        }
                    }
                    throw ex;
                }
            }
        }
            
        //    using (ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory())
        //    {
        //        using (ISession session = sessionFactory.OpenSession())
        //        {
        //            using (ITransaction transaction = session.BeginTransaction())
        //            {
        //                try
        //                {
        //                    foreach (T o in objects)
        //                    {
        //                        session.Save(o);
        //                    }
        //                    transaction.Commit();
        //                }
        //                catch (Exception ex)
        //                {
        //                    log.Warn(ex.Message, ex);
        //                    if (sessionFactory != null && session != null)
        //                    {
        //                        if (transaction.IsActive == true)
        //                        {
        //                            transaction.Rollback();
        //                        }
        //                    }
        //                    throw ex;
        //                }
        //            }
        //        }
        //    }
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objects"></param>
        public override void Update<T>(params T[] objects)
        {
            using (ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory())
            {
                using (ISession session = sessionFactory.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        try
                        {
                            foreach (T o in objects)
                            {
                                session.Update(o);
                            }
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            log.Warn(ex.Message, ex);
                            if (sessionFactory != null && session != null)
                            {
                                if (transaction.IsActive == true)
                                {
                                    transaction.Rollback();
                                }
                            }
                            throw ex;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objects"></param>
        public override void Delete<T>(params T[] objects)
        {
            using (ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory())
            {
                using (ISession session = sessionFactory.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        try
                        {
                            foreach (T o in objects)
                            {
                                session.Delete(o);
                            }
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            log.Warn(ex.Message, ex);
                            if (sessionFactory != null && session != null)
                            {
                                if (transaction.IsActive == true)
                                {
                                    transaction.Rollback();
                                }
                            }
                            throw ex;
                        }
                    }
                }
            }
        }
    }
}
